package com.company.client;

import com.company.common.User;

import java.util.Scanner;

public class IMClient {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean flag = false;
        do {
            System.out.println("====== 欢迎使用xxx网络通信系统 ======");
            System.out.println("\t\t 1.登录系统 \t\t");
            System.out.println("\t\t 2.注册账号 \t\t");
            System.out.println("\t\t 9.退出系统 \t\t");
            System.out.println("请输入你的选择：");
            String num = scanner.next();
            if ("1".equals(num)) {
                System.out.println("请输入用户号：");
                String name = scanner.next();
                System.out.println("请输入密码：");
                String pwd = scanner.next();
                User user = new User(name, pwd);
                flag = UserService.login(user);
                if (flag) {
                    MessageService.getOfflineMsg(user.getName());
                    do {
                        System.out.println("====== 欢迎使用xxx网络通信系统 ======");
                        System.out.println("====== 二级菜单，欢迎" + user.getName() + "使用 ======");
                        System.out.println("\t\t 1.在线列表 \t\t");
                        System.out.println("\t\t 2.群发消息 \t\t");
                        System.out.println("\t\t 3.私聊消息 \t\t");
                        System.out.println("\t\t 4.发送文件 \t\t");
                        System.out.println("\t\t 5.退出系统 \t\t");
                        System.out.println("请输入你的选择：");
                        num = scanner.next();
                        switch (num) {
                            case "1":
                                UserService.getOnline(user.getName());
                                break;
                            case "2":
                                System.out.println("请输入想说的话：");
                                MessageService.sendToMany(scanner.next(), user.getName());
                                break;
                            case "3":
                                System.out.println("请输入想聊天的用户号（在线/离线）：");
                                String username = scanner.next();
                                System.out.println("请输入想说的话：");
                                String content = scanner.next();
                                MessageService.sendToOne(content, username, user.getName());
                                break;
                            case "4":
                                System.out.println("请输入想发送文件给的用户号（在线/离线）：");
                                username = scanner.next();
                                System.out.println("请输入要发送文件的完整路径：");
                                String fromPath = scanner.next();
                                System.out.println("请输入对方接受文件的完整路径：");
                                String toPath = scanner.next();
                                FileService.copyFromLocal(fromPath, toPath, username, user.getName());
                                break;
                            case "5":
                                flag = false;
                                UserService.exit(user.getName());
                                System.exit(0);
                                break;
                        }
                    } while (flag);
                }
            } else if ("2".equals(num)) {
                System.out.println("请输入用户号：");
                String name = scanner.next();
                System.out.println("请输入密码：");
                String pwd = scanner.next();
                User user = new User(name, pwd);
                flag = UserService.register(user);
                if (flag) {
                    System.out.println(user.getName() + "，注册成功，请牢记您的密码！");
                    flag = false;
                }
            } else if ("9".equals(num)) {
                flag = true;
            }
        } while (!flag);
    }
}
